<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="分类名称">
        <el-input size="small" v-model="listQuery.q_name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select size="small" v-model="listQuery.q_state" placeholder="请选择">
          <el-option v-for="item in optStateValues" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh-left" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="passBtn" v-permission="['admin/product/cate/auditPass']">审核通过</el-button>
      <el-button size="small" type="primary" @click="rejectBtn" v-permission="['admin/product/cate/auditPass']">审核驳回</el-button>
    </div>
    <el-table
      ref="singleTable"
      :data="dataList"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleCurrentChange">
      <el-table-column type="selection" align="center" width="50"></el-table-column>
      <el-table-column show-overflow-tooltip property="pathName" label="分类名称"></el-table-column>
      <el-table-column width="160" property="sellerName" label="商家名称"></el-table-column>
      <el-table-column width="160" property="createTime" label="申请时间"></el-table-column>
      <el-table-column width="160" label="审核状态">
        <template slot-scope="scope">
          {{ codeBox.SELLER_CATE_STATE[scope.row.status] }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
import { auditList, auditPass } from "@/api/product";
import { getCodeManager, codeToArray } from "@/api/all";
import Pagination from "@/components/Pagination";
import { Message } from "element-ui";
export default {
  name: "Brand",
  components: { Pagination },
  data() {
    return {
      dataList: [],
      optStateValues: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        q_name: "",
        q_state: "",
      },
      currentRow: null,
    };
  },
  created() {
    getCodeManager("SELLER_CATE_STATE", () => {
      this.optStateValues = codeToArray("SELLER_CATE_STATE");
    });
    getCodeManager("SELLER_CATE_STATE", () => {});
    this.getList();
  },
  methods: {
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val;
    },
    // 获取列表数据
    getList() {
      this.listLoading = true;
      auditList(this.listQuery).then((response) => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery.q_name = "";
      this.listQuery.q_state = "";
      this.getList();
    },
    passBtn() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      let ids = "";
      for (let i in this.currentRow) {
        if (this.currentRow[i].status != 1) {
          Message.error("只有[提交审核]的记录可以审核");
          return;
        }
        if (this.currentRow.length > 1) {
          ids += this.currentRow[i].id + ",";
        } else {
          ids = this.currentRow[0].id;
        }
      }
      this.$confirm("确定要审核通过吗？", "提示", {
        onfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        auditPass({ type: 2, ids: ids }).then((res) => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        });
      });
    },
    rejectBtn() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      let ids = "";
      for (let i in this.currentRow) {
        if (this.currentRow[i].status != 1) {
          Message.error("只有[提交审核]的记录可以审核");
          return;
        }
        if (this.currentRow.length > 1) {
          ids += this.currentRow[i].id + ",";
        } else {
          ids = this.currentRow[0].id;
        }
      }
      this.$confirm("确定要将商家的申请驳回吗？", "提示", {
        onfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        auditPass({ type: 3, ids: ids }).then((res) => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        });
      });
    },
  },
};
</script>
<style scoped>
.filter-container .el-col-6 {
  margin-bottom: 15px;
}
.filter-item {
  margin-bottom: 0;
}
/deep/ .filter-container label {
  font-size: 14px;
  font-weight: 500;
  margin-right: 10px;
}
</style>
